<?php
namespace WebApper\Shortcode;

/*
 * [clientPortal]
 *
 */
 class ClientPortal extends \WebApper\Shortcode {
	
    /**
     * Define shortcode properties
     *
     */
	protected $shortcode = 'clientPortal';
	
    /**
     * Handles the add post shortcode
     *
     * @param array $atts
     */
	/*
	
    */
	
	public function shortcode( $atts ) {
			global $wpdb, $webapper, $current_user;
			$user_id = $current_user->ID;
			if(isset($_POST['saveCardInfo']))
			{
				if(!empty($_POST['cardholdername']) && !empty($_POST['CardType']) && !empty($_POST['txtCreditCardNumber']) && !empty($_POST['cardExpirationMonth']) && !empty($_POST['cardExpirationYear']) && !empty($_POST['Country']) && !empty($_POST['Address']) && !empty($_POST['City']) && !empty($_POST['State']) && !empty($_POST['cardCCV']) && !empty($_POST['txtZipCode']) && !empty($_POST['bankRoutingNumber']) && !empty($_POST['accountNumber']) && !empty($_POST['checkNumber']))
				{					
					$cardHolderName 		= trim($_POST['cardholdername']);
					$cardType 				= trim($_POST['CardType']);	
					$txtCreditCardNumber 	= base64_encode(trim($_POST['txtCreditCardNumber']));	
					$cardExpirationMonth 	= trim($_POST['cardExpirationMonth']);	
					$cardExpirationYear 	= trim($_POST['cardExpirationYear']);
					$Country 				= trim($_POST['Country']);	
					$Address 				= trim($_POST['Address']);
					$City 					= trim($_POST['City']);	
					$State 					= trim($_POST['State']);
					$cardCCV 				= base64_encode(trim($_POST['cardCCV']));
					$txtZipCode 			= trim($_POST['txtZipCode']);	
					$bankRoutingNumber 		= base64_encode(trim($_POST['bankRoutingNumber']));
					$accountNumber 			= base64_encode(trim($_POST['accountNumber']));	
					$checkNumber 			= base64_encode(trim($_POST['checkNumber']));
					$user_payment_master_id = $_POST['hidden_payment_master_id'];
					if($_POST['hidden_payment_master_id']==''){
						
						 web_apper_insert_transaction_master($user_id,'','','','','','','','',$cardHolderName,$cardType,$txtCreditCardNumber,$cardExpirationMonth,$cardExpirationYear,'',$Country, $Address,$City,$State,$cardCCV,$txtZipCode,$bankRoutingNumber,$accountNumber,$checkNumber);					
					}
					else{
						web_apper_update_transaction_master($user_payment_master_id, $user_id,'','','','','','','','',$cardHolderName,$cardType,$txtCreditCardNumber,$cardExpirationMonth,$cardExpirationYear,'',$Country, $Address,$City,$State,$cardCCV,$txtZipCode,$bankRoutingNumber,$accountNumber,$checkNumber);
					}									
				}
			}
			
//--------------------------------  FETCHING DATA FROM USER PAYMENT MASTER  -------------------------------------
  		
		$payment_master = web_apper_get_payment_master($user_id);
		//print_r($payment_master);
		$signedUp_date    	    = $payment_master ->start_date;
		$expire_date    	    = $payment_master ->end_date;
		$user_payment_master_id = $payment_master ->user_payment_master_id;
		$card_holderName    	= $payment_master ->card_holder_name;
		$card_type         	    = $payment_master ->card_type;
		$card_number        	= $payment_master ->card_number;
		$cardExpire_month   	= $payment_master ->card_expiration_month;
		$cardExpire_year   	    = $payment_master ->card_expiration_year;
		$country            	= $payment_master ->card_country;
		$address            	= $payment_master ->card_address;
		$city               	= $payment_master ->card_city;
		$state              	= $payment_master ->card_state;
		$card_ccv           	= $payment_master ->card_ccv;
		$zip_code  				= $payment_master ->card_zip_code;
		$bank_routing_number	= $payment_master ->bank_routing_number;
		$account_number 		= $payment_master ->bank_account_number;
		$check_number 			= $payment_master ->bank_check_number;
			
//------------------------------  END FETCHING DATA FROM USER PAYMENT MASTER  ------------------------------------------
//--------------------------------  FETCHING DATA FROM USER PAYMENT TRANSACTION  ---------------------------------------
		$payment_transaction          = web_apper_get_payment_transaction($user_id);
		$user_payment_transaction_id  = $payment_transaction -> user_payment_transaction_id;
		$user_id  					  = $payment_transaction -> user_id;
		$payment_for  				  = $payment_transaction -> payment_for;
		$received_as  				  = $payment_transaction -> received_as;
		$amount_paid  				  = $payment_transaction -> amount_paid;
		$transaction_date  			  = $payment_transaction -> transaction_date;
		$transaction_status  		  = $payment_transaction -> transaction_status;
		$created_on  				  = $payment_transaction -> created_on;
		$edited_on  				  = $payment_transaction -> edited_on;
//--------------------------------  END OF FETCHING DATA FROM USER PAYMENT TRANSACTION  -----------------------------------
// ---  GET ALL COUNTRIES --------------------------------------------
$countrys = web_apper_get_all_countries();

?>
<h3> Client Portal</h3>
<form class="form-horizontal">
  <div class="control-group">
    <label class="control-label" for="PaymentCleared">Payment Cleared</label>
    <div class="controls">
      <label class="control-label" for="PaymentCleared"><?php echo count($user_payment_transaction_id) ;?></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Payment Remaining">Payment Remaining</label>
    <div class="controls">
      <label class="control-label" for="Payment Remaining"><?php echo "payment remaining" ;?></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Amount Paid">Amount Paid(Cleared)</label>
    <div class="controls">
      <label class="control-label" for="Amount Paid"><?php echo $amount_paid ;?></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Balance">Balance</label>
    <div class="controls">
      <label class="control-label" for="Balance"></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Default amount">Default amount</label>
    <div class="controls">
      <label class="control-label" for="Default amount"></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Date signed Up">Date signed Up</label>
    <div class="controls">
      <label class="control-label" for="Date signed Up"><?php echo date( "d-M-Y", strtotime($signedUp_date)) ;?></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Membership Expiration">Membership Expiration</label>
    <div class="controls">
      <label class="control-label" for="Membership Expiration"><?php echo date( "d-M-Y", strtotime($expire_date)) ;?></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Membership Expiration">Next Due Date</label>
    <div class="controls">
      <label class="control-label" for="Membership Expiration"></label>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="Membership Expiration">Reward Points earned</label>
    <div class="controls">
      <label class="control-label" for="Membership Expiration"></label>
    </div>
  </div>
</form>
<div style="float:left">
  <div style="float:left">
  <h3>Debit/Credit Cards</h3>
  <form class="form-horizontal" action=""  method="post" name="debitCreditCards">
    <div class="control-group">
      <input type="hidden" name="hidden_payment_master_id" value="<?php echo $user_payment_master_id; ?>">
      <label class="control-label" for="Principal Amount">Name on Card</label>
      <div class="controls">
        <input type="text" id="cardholdername"  name="cardholdername" value="<?php echo $card_holderName; ?>" placeholder="Card Holder Name"  required="required">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Credit Card Type">Credit Card Type</label>
      <div class="controls">
        <select name="CardType" id="CardType" class="selectpicker" data-live-search="true" data-size="5" >
          <?php
				$creditCardTypeArray = array('Visa','MasterCard','American Express','Discover');
				foreach($creditCardTypeArray as $creditCard)
				{
					if($creditCard == $card_type)
					{
						echo "<option value=".$creditCard." selected>".$creditCard."</option>";
					}
					else
					{
						echo "<option value=".$creditCard.">".$creditCard."</option>";
					}
				}
			?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Card Number">Card Number</label>
      <div class="controls">
        <input type="text" id="txtCreditCardNumber"  name="txtCreditCardNumber" value="<?php echo $card_number; ?>"  placeholder="Card Number"  required="required">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Card Expiration Date">Card Expiration Date</label>
      <div class="controls">
        <select name="cardExpirationMonth" id="cardExpirationMonth" class="selectpicker" data-live-search="true" data-size="5" >
          <?php
				$monthArray =array('01','02','03','04','05','06','07','08','09','10','11','12'); 
				foreach($monthArray as $month)
				{
					if($month == $cardExpire_month)
					{
						echo "<option value=\"".$month."\" selected='selected'>".$month."</option>";
					}
					else
					{
						echo "<option value=\"".$month."\">".$month."</option>";
					}
				}
            ?>
        </select>
        <select name="cardExpirationYear"  id="cardExpirationYear" class="selectpicker" data-live-search="true" data-size="5" >
          <?php
				$start_year = date('Y');
				$end_year = date('Y', strtotime($start_year .' +15 Year'));		
				$selected = is_null($selected) ? date('Y') : $selected;
				$r = range($start_year, $end_year);
				foreach( $r as $year )
				{
					
					if($cardExpire_year !='')
					{
						$select .= "<option value=\"$year\"";
						$select .= ($year ==$cardExpire_year) ? ' selected="selected"' : '';
						$select .= ">$year</option>\n";
					}
					else
					{
						$select .= "<option value=\"$year\"";
						$select .= ($year ==$selected) ? ' selected="selected"' : '';
						$select .= ">$year</option>\n";
					}
				}
				echo  $select;
            ?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Country">Country:</label>
      <div class="controls">
        <select name="Country"  id="Country" class="selectpicker" data-live-search="true" data-size="5" >
          <?php foreach($countrys as $countrynameFull => $countrynameShort){
						if($countrynameShort == $country)
						{
							$selectcountry .= '<option  value="'.$countrynameShort.'" selected>'.$countrynameFull.'</option>';
						}
						else
						{
							$selectcountry .='<option  value="'.$countrynameShort.'">'.$countrynameFull.'</option>';
						}		
					  }
					  echo  $selectcountry; 
            	?>
        </select>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Address">Address:</label>
      <div class="controls">
        <textarea name="Address" id="Address" rows="3" cols="3" ><?php echo $address; ?></textarea>
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="City">City:</label>
      <div class="controls">
        <input type="text" id="City" required name="City" placeholder="City" value="<?php echo $city; ?>" >
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="State">State:</label>
      <div class="controls">
        <input type="text" id="State" required name="State" placeholder="State"  value="<?php echo $state; ?>" >
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Card CCV">Card CCV</label>
      <div class="controls">
        <input type="password" id="cardCCV" maxlength="5" name="cardCCV" value="<?php echo $card_ccv; ?>" placeholder="Card CCV"  required="required">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="Card Billing Zip">Card Billing Zip</label>
      <div class="controls">
        <input type="text" id="txtZipCode" name="txtZipCode" value="<?php echo $zip_code; ?>" placeholder="Card Billing Zip"  required="required" onchange="return     CheckZipCode(this.value) " onblur=" return CheckZipCode(this.value)  ">
      </div>
    </div>
    <div class="control-group">
      <div class="controls">
        <button type="submit" name="saveCardInfo" id="saveCardInfo" class="btn btn-primary">Save</button>
      </div>
    </div>
    </div>
    <div  style="float:right; margin-left:100px">
    <h3>Electronic Checking</h3>
    <div class="form-horizontal" >
      <div class="control-group">
        <label class="control-label" for="Principal Amount">Bank Routing Number</label>
        <div class="controls">
          <input type="text" id="bankRoutingNumber" name="bankRoutingNumber" value="<?php echo $bank_routing_number; ?>" placeholder="Bank Routing Number"  required="required">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="Principal Amount">Account Number</label>
        <div class="controls">
          <input type="text" id="accountNumber"  name="accountNumber" value="<?php echo $account_number; ?>" placeholder="Account Number"  required="required">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="Check Number">Check Number</label>
        <div class="controls">
          <input type="text" id="checkNumber"  name="checkNumber" value="<?php echo $check_number; ?>" placeholder="Check Number"  required="required">
        </div>
      </div>
    </div>
  </form>
</div>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
	$('.selectpicker').selectpicker();
});
function CheckZipCode(strString)
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) return false;
// test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			alert("Please type in correct zipcode format.");
		    document.getElementById('txtZipCode').value='';
		    document.getElementById('txtZipCode').focus();
			blnResult = false;
		}
	}
	return blnResult;
}

function CheckCreditCardNumber(strString)
{
	var reCreditCard = /[\.\,\?!\s]/g;
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	if (strString.length != 16)
	{
		alert('You entered an invalid credit card number. Please try again.');
		document.getElementById('txtCreditCardNumber').value='';
		document.getElementById('txtCreditCardNumber').focus();
		return false;
	}
	if(reCreditCard.test(strString))
	{
		alert("Numeric characters only with no spaces or punctutation.");
		document.getElementById('txtCreditCardNumber').value='';
		document.getElementById('txtCreditCardNumber').focus();
		return false;
	}
	
	var sum = 0; 
	var numdigits = strString.length; 
	var parity = numdigits % 2; 
	for(var i=0; i < numdigits; i++)
	{ 
		var digit = parseInt(strString.charAt(i)) 
		if(i % 2 == parity)
		digit *= 2; 
		if(digit > 9) 
		digit -= 9;
		sum += digit;
	}
	if((sum % 10) != 0)
	{
		alert('You entered an invalid credit card number. Please try again.');
		document.getElementById('txtCreditCardNumber').value='';
		document.getElementById('txtCreditCardNumber').focus();
		return false;
	}
// test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			alert("Numeric characters only with no spaces or punctutation.");
		    document.getElementById('txtCreditCardNumber').value='';
		    document.getElementById('txtCreditCardNumber').focus();
			blnResult = false;
		}
	}
	return blnResult;
}

function validate(event) {
 var key = window.event ? event.keyCode : event.which;

    if (event.keyCode == 8 || event.keyCode == 46
     || event.keyCode == 37 || event.keyCode == 39) {
        return true;
    }
   var theEvent = event || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\./;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}

</script>
<?php	}
			
}
$initialize = new clientPortal(); 

?>
